import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Hoem from '../components/Home.vue'
import Welcome from '../components/Welcome.vue'
import Users from '../components/items/Users.vue'
import Categories from '../components/items/goods/Categories.vue'
import Goods from '../components/items/goods/Goods.vue'
import Orders from '../components/items/Orders.vue'
import Params from '../components/items/goods/Params.vue'
import Reports from '../components/items/Reports.vue'
import Rights from '../components/items/rights/Rights.vue'
import Roles from '../components/items/rights/Roles.vue'
import AddGood from '../components/items/goods/AddGood.vue'
Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    component: Login
  },
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/home',
    component: Hoem,
    redirect: '/welcome',
    children: [
      {
        component: Welcome,
        path: '/welcome'
      },
      {
        component: Users,
        path: '/users'
      },
      {
        component: Categories,
        path: '/categories'
      },
      {
        component: Goods,
        path: '/goods'
      },
      {
        component: Orders,
        path: '/orders'
      },
      {
        component: Params,
        path: '/params'
      },
      {
        component: Reports,
        path: '/reports'
      },
      {
        component: Rights,
        path: '/rights'
      },
      {
        component: Roles,
        path: '/roles'
      },
      {
        component: AddGood,
        path: '/goods/add'
      }
    ]
  }
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})
export default router
